<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>员工管理</title>
    <#--freemarker没有上下文概念,使用其他路径去找模板文件-->
    <#include "../common/link.ftl">
    <script>
        $(function () {
            $('.btn_delete').click(function () {
                var id = $(this).data('id');
                $.messager.confirm("警告","确定要删除吗?",function () {
                    $.get('/employee/delete.do',{id:id},handlerMessage);
                })
            })
            //给导入按钮绑定点击事件 , 事件中弹出带有文件上传表单的模态框
            $(".btn-import").click(function () {
                $("#importModal").modal('show');
            })
            //导入文件保存按钮
            $(".btn-submit").click(function () {
                $("#importForm").ajaxSubmit(function (data) {
                    if (data.success){
                        $.messager.alert("提示","导入成功")
                        window.setTimeout(function () {
                            window.location.href = "/employee/list.do"
                        },2000)
                    }else{
                        $.messager.popup(data.msg);
                    }
                })
            })
            //全选复选框点击事件
            $('#allCb').click(function () {
                //获取复选框allCb的点击状态
                var checked = $(this).prop('checked');
                $('.cb').prop('checked',checked);
            })
            //列表复选框点击事件
            $('.cb').click(function () {
                //获取列表已经被勾选的选项框数量,如果等于全部选项框的长度全选复选框被勾选,反之
                var flag = ($('.cb:checked').length == $('.cb').length);
                $('#allCb').prop('checked',flag);
            })
            //给批量删除按钮添加点击事件
            $('.btn-batchDelete').click(function () {
                //如果选中长度为0,弹出提示
                var cbs = $(".cb:checked")
                if (cbs.length ==0){
                    $.messager.alert("提示","请先选中数据");
                    return;
                }
                //有选中的数据
                var ids = [];
                cbs.each(function (i,domEle) {
                    var id = $(domEle).data("id");
                    ids.push(id);
                });
                //获取到的id数组 提示是否删除,然后发送异步请求
                $.messager.confirm("提示","是否确定删除?",function () {
                    $.get('/employee/batchDelete.do',{ids:ids},handlerMessage)
                })
            });

            //部门查询选项框添加事件
            $('#deptSearch').click(function () {
                var checked = $(this).prop('checked');
                //console.log(checked);
                var deptId = '<@shiro.principal property="deptId"/>';
                console.log(deptId);
                if (checked){
                    $('#dept').val(deptId);
                }else{
                    $('#dept').val('-1');
                }

            })

            //设置用户状态
            $('.btn_status').click(function () {
                var status = $(this).data("status")
                var id = $(this).data("id")
                $.get('/employee/changeStatus.do',{status:status,id:id},function (data) {
                    if (data.success){
                        window.location.reload();
                    }else {
                        $.messager.popup(data.msg);
                    }
                })
            })
        })
    </script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <#include "../common/navbar.ftl">
    <!--菜单回显-->
    <#assign currentMenu="employee"/>
    <#include "../common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>员工管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <!--高级查询--->
                <div style="margin: 10px;">
                    <form class="form-inline" id="searchForm" action="/employee/list.do" method="post">
                        <input type="hidden" name="currentPage" id="currentPage" value="1">
                        <div class="form-group">
                            <label for="keyword">关键字:</label>
                            <input type="text" class="form-control" id="keyword" name="keyword" value="${qo.keyword!}" placeholder="请输入姓名/邮箱">
                        </div>
                        <div class="form-group">
                            <label for="dept"> 部门:</label>
                            <select class="form-control" id="deptId" name="deptId">
                                <option value="-1">全部</option>
                                <#list departments! as d>
                                    <option value="${d.id!}">${d.name!}</option>-->
                                </#list>
                            </select>
                            <script>
                                <#--$("#dept").val(${(qo.deptId)!})-->
                                $("#deptId option[value='${(qo.deptId)!}']").prop("selected", true);
                            </script>
                        </div>
                        <button id="btn_query" class="btn btn-primary">
                            <span class="glyphicon glyphicon-search"></span> 查询</button>

                        <a href="/employee/input.do" class="btn btn-success btn_redirect">
                            <span class="glyphicon glyphicon-plus"></span> 添加
                        </a>
                        <a href="#" class="btn btn-danger btn-batchDelete">
                            <span class="glyphicon glyphicon-trash"></span> 批量删除
                        </a>
                        <a href="/employee/exportXls.do" class="btn btn-warning" >
                            <span class="glyphicon glyphicon-download"></span> 导出
                        </a>
                        <a href="#" class="btn btn-warning btn-import">
                            <span class="glyphicon glyphicon-upload"></span> 导入
                        </a><br/>
                        <div class="form-group" id="deptSearchDiv">
                            <label for="depts" class="col-xs-9 control-label">只查看本部门：</label>
                            <div class="col-sm-1"style="margin-left: 15px;">
                                <input type="checkbox" id="deptSearch" name="deptSearch" class="checkbox" >
                                <script>
                                    $("#deptSearch").prop("checked", $('#dept').val() == <@shiro.principal property="deptId"/>);
                                </script>
                            </div>
                        </div>
                    </form>
                </div>
                <table class="table table-hover table-bordered">
                    <thead>
                    <tr>
                        <th><input type="checkbox" id="allCb"></th>
                        <th>编号</th>
                        <th>名称</th>
                        <th>email</th>
                        <th>年龄</th>
                        <th>管理员</th>
                        <th>部门</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <#list pageInfo.list as employee>
                        <tr>
                            <td><input type="checkbox" class="cb" data-id="${(employee.id)!}"></td>
                            <td>${employee_index +1}</td>
                            <td>${(employee.name)!}</td>
                            <td>${(employee.email)!}</td>
                            <td>${(employee.age)!}</td>
                            <td>${(employee.admin?string("是","否"))!}</td>
                            <td>${(employee.dept.name)!}</td>
                            <td>${(employee.status?string("正常","禁用"))!}</td>
                            <td>
                                <a href="/employee/input.do?id=${employee.id!}" class="btn btn-info btn-xs btn_redirect">
                                    <span class="glyphicon glyphicon-pencil"></span> 编辑
                                </a>
                                <a href="#" data-id="${employee.id!}" class="btn btn-danger btn-xs btn_delete">
                                    <span class="glyphicon glyphicon-trash"></span> 删除
                                </a>
                                <@shiro.hasRole name="admin">
                                <#if (employee.status)!>
                                <a href="#" data-id="${employee.id!}" data-status="0" class="btn btn-warning btn-xs btn_status">
                                    <span class="glyphicon glyphicon-minus-sign"></span> 禁用
                                </a>
                                <#else>
                                <a href="#" data-id="${employee.id!}" data-status="1" class="btn btn-success btn-xs btn_status">
                                    <span class="glyphicon glyphicon-tag"></span> 恢复
                                </a>
                                </#if>
                                    <a href="/employee/resetPwd.do?id=${employee.id!}" class="btn btn-warning btn-xs btn_reset">
                                        <span class="glyphicon glyphicon-tags"></span> 重置密码
                                    </a>
                                </@shiro.hasRole>
                            </td>
                        </tr>
                    </#list>
                </table>
                <!--分页-->
                <#include "../common/page.ftl">
            </div>
        </section>
    </div>
    <#include "../common/footer.ftl">
</div>
<#--导入模态框-->
<div class="modal fade" id="importModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">导入</h4>
            </div>
            <form class="form-horizontal" action="/employee/importXls.do" method="post" id="importForm">
            <div class="modal-body">

                    <input type="hidden" name="id">
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-3 control-label"></label>
                        <div class="col-sm-6">
                            <input type="file" name="file">
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <div class="col-sm-3"></div>
                        <div class="col-sm-6">
                            <a href="/xlstemplates/employee_import.xls" class="btn btn-success" >
                                <span class="glyphicon glyphicon-download"></span> 下载模板
                            </a>
                        </div>
                    </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary btn-submit">保存</button>
            </div>
            </form>
        </div>
    </div>
</div>

</body>
</html>
